package com.rw.coco.reply.reader;

import com.rw.coco.reply.constants.AdvanceReplyKey;
import com.rw.tool.util.properties.PropertiesReader;
import com.rw.tool.util.string.StringFormatter;

import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 可可回复专用读取器
 */
public class CocoPropertiesReader {
    private final List<PropertiesReader> propertiesReaders;

    public CocoPropertiesReader(List<URL> urlList) {
        propertiesReaders = urlList.stream()
                .map(PropertiesReader::new)
                .collect(Collectors.toList());
    }

    public String get(String key) {
        // 获取原始回复和机器人名称
        String reply = find(key);
        String botName = find(AdvanceReplyKey.BOT_NAME);

        return StringFormatter.replaceAll(reply, botName);
    }

    /**
     * 在配置文件列表中找到对应的值
     */
    private String find(String key) {
        for (PropertiesReader propertiesReader : propertiesReaders) {
            String value = propertiesReader.get(key);
            if (value != null) {
                return value;
            }
        }

        return null;
    }
}
